﻿/**
 * Created by Administrator on 2015/7/1.
 */

/*window.onerror = function (err) {
 log('window.onerror: ' + err)
 }*/


/*初始化JsBridge*/
/*
 function connectWebViewJavascriptBridge(callback) {
 if (window.WebViewJavascriptBridge) {
 callback(WebViewJavascriptBridge);
 } else {
 document.addEventListener('WebViewJavascriptBridgeReady', function () {
 callback(WebViewJavascriptBridge);
 }, false);
 }
 }

 /!*关闭页面*!/
 function CloseWebPage() {
 WebViewJavascriptBridge.callHandler(
 'closeWebView',
 {},
 function (responseData) {
 }
 );
 }
 /!*分享功能*!/
 function share() {
 WebViewJavascriptBridge.callHandler(
 'UmengShare',
 {'shareType': '2', 'title': '测试title', 'shareContent': '给你推荐一个好玩的APP，名字叫“球友圈”，能订场、能约球、约教练，还能围观比赛！猛戳我下载'},
 function (responseData) {
 alert(responseData);
 }
 );
 alert('1111');
 }

 connectWebViewJavascriptBridge(function (bridge) {
 bridge.init(function (message, responseCallback) {
 //log('JS got a message', message)
 var data = {'Javascript Responds': 'Wee!'}
 //log('JS responding with', data)
 responseCallback(data)
 });
 });
 */


var JsBridge = {
    connectWebViewJavascriptBridge: function (callback) {
        if (window.WebViewJavascriptBridge) {
            callback(WebViewJavascriptBridge);
        } else {
            document.addEventListener('WebViewJavascriptBridgeReady', function () {
                callback(WebViewJavascriptBridge);
            }, false);
        }
    },
    JsBridgeInit: function () {
        this.connectWebViewJavascriptBridge(function (bridge) {
            bridge.init(function (message, responseCallback) {
                //log('JS got a message', message)
                var data = {'Javascript Responds': 'Wee!'}
                //log('JS responding with', data)
                responseCallback(data);
            });
        })
    },
    activityOrderDetail: function (data, callback) {
        if (typeof callback !== "function") {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'activityOrderDetail',
            data || {},
            function (responseData) {
                if (callback && responseData) {
                    callback(responseData);
                }
            });
    },
    choosePics: function (data, callback) {
        if (typeof callback !== "function") {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'choosePics',
            data || {},
            function (responseData) {
                  if(callback && responseData){
                 callback(responseData);
                 }
            });
    },
    publishActivityComment: function (data, callback) {
        if (typeof callback !== "function") {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'publishActivityComment',
            data || {},
            function (responseData) {
                if(callback && responseData){
                    callback(responseData);
                }
            });
    },
    getUserinfo: function (data, callback) {
        if (typeof callback !== "function") {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'getUserInfo',
            data || {},
            function (responseData) {
                if(callback && responseData){
                    callback(responseData);
                }
            });
    },
    RaceUnPay: function (data, callback) {
        if (typeof callback !== "function") {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'RaceUnPay',
            data || {},
            function (responseData) {
                if(callback && responseData){
                    callback(responseData);
                }
            });
    },
    BackToApp: function () {
        WebViewJavascriptBridge.callHandler(
            'closeWebView',
            {},
            function (responseData) {
            });
    },
    UmengShare: function (data, callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'UmengShare',
            data || {}, //{'shareType': '2', 'title': '测试title', 'shareContent':'给你推荐一个好玩的APP，名字叫“球友圈”，能订场、能约球、约教练，还能围观比赛！猛戳我下载'},
            function (responseData) {
                if (callback && responseData) {
                    callback(responseData);
                }
            }
        );
    },
    GetPicturesList: function (callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'choosePics',
            {},
            function (responseData) {
                if (callback && responseData) {
                    callback(responseData);
                }
            }
        );
    },
    GetToken: function (callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'getToken',
            {},
            function (responseData) {
                if (callback && responseData) {
                    callback(responseData);
                }
            }
        );
    },
    refreshCoupon: function (data, callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'refreshCoupon',
            data || {},
            function (responseData) {
                if (callback && responseData) {
                    callback(responseData);
                }
            }
        );
    },
    GetUserInfo: function (callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'getUserInfo',
            {},
            function (responseData) {
                callback(responseData);
            }
        );
    },
    Pay_AliPay: function (data, callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge(
            'AliPay',
            data || {},//{"statiumName":"xxxx","statiumAddress":"yyyy","couponId":"ba66c243230845728ce366b8fc38198e","orderId":"ba66c243230845728ce366b8fc38198e"}
            function (responseData) {
                callback(responseData);//{"result":"false","reson":"传递的订单信息不够完整，无法进行支付"}
            }
        );
    },
    Pay_WXPay: function (data, callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'WXPay',
            data || {},//{"statiumName":"xxxx","couponId":"ba66c243230845728ce366b8fc38198e","orderId":"ba66c243230845728ce366b8fc38198e"}
            function (responseData) {
                callback(responseData);////{"result":"false","reson":"传递的订单信息不够完整，无法进行支付"}
            }
        );
    },
    //获取用户登录信息
    getUserLoginInfo: function (data, callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'getUserLoginInfo',
            data || {},
            function (responseData) {
                if (callback && responseData) {
                    callback(responseData);
                }
            }
        )
    },
    //通知app以native方式打开新h5页面
    openNewPageH5: function (data, callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'openNewPageH5',
            data || {},
            function (responseData) {
                if (callback && responseData) {
                    callback(responseData);
                }
            }
        )
    },
    //通知app跳转新native页面
    openNewPageNative: function (data, callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.callHandler(
            'openNewPageNative',
            data || {},
            function (responseData) {
                if (callback && responseData) {
                    callback(responseData);
                }
            }
        )
    },

    //js获取从native传递过来的数据
    GetMsgFromNative: function (callback) {
        if (typeof callback !== 'function') {
            callback = false;
        }
        WebViewJavascriptBridge.registerHandler('GetMsgFromNative', function (data, response) {
            if (data && callback) {
                callback(data);
            }
            //response.respondWith({ 'Javascript Says':'Right back atcha!' });
        })
    }


};

